package cn.itcast.bos.web.action.take_delivery;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.utils.FileUtils;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
public class WayBillAction extends BaseAction<WayBill> {
	
	@Autowired
	private WayBillService wayBillService;
	
	@Action("wayBillAction_save")
	public String save() throws Exception {
		try {
			wayBillService.save(model);
		} catch (Exception e) {
			e.printStackTrace();
			ServletActionContext.getResponse().getWriter().write("0");
		}
		return NONE;
	}
	
	
	/**
	  * @Description: 运单模板导出（excel文件形式），将文件下载
	 */
	@Action("wayBillAction_exportXls")
	public String exportXls() throws Exception {
		//创建空白POI excel文件对象
		//XSSFWorkbook; 07
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建标签页
		HSSFSheet sheet = workbook.createSheet("运单数据");

		sheet.setDefaultColumnWidth(12);
		//sheet.setColumnWidth(2, 16);
		sheet.setDefaultRowHeight((short) (1.5 * 256));
		//创建行  1、创建标题行   2、创建数据行
		
		HSSFCellStyle style = workbook.createCellStyle();
		HSSFFont f  = workbook.createFont();      
	    f.setFontHeightInPoints((short) 11);//字号      
	    f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗     
	  
	    style.setFont(f);      
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中      
	    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中 
	   
	    HSSFRow headRow = sheet.createRow(0);
	    headRow.setRowStyle(style);
		headRow.setHeight((short) (1.5 * 256));
		
		
		//创建单元格--写入单元格数据
		HSSFCell hssfCell0 = headRow.createCell(0);
		hssfCell0.setCellValue("编号");
		hssfCell0.setCellStyle(style);
		
		HSSFCell hssfCell1 = headRow.createCell(1);
		hssfCell1.setCellValue("产品");
		hssfCell1.setCellStyle(style);
		
		HSSFCell hssfCell2 = headRow.createCell(2);
		hssfCell2.setCellValue("快递产品类型");
		hssfCell2.setCellStyle(style);
		
		/*HSSFCell hssfCell10 = headRow.createCell(10);
		hssfCell10.setCellValue("快递产品类型");
		hssfCell10.setCellStyle(style);*/
		
		HSSFCell hssfCell3 = headRow.createCell(3);
		hssfCell3.setCellValue("发件人姓名");
		hssfCell3.setCellStyle(style);
		
		HSSFCell hssfCell4 = headRow.createCell(4);
		hssfCell4.setCellValue("发件人电话");
		hssfCell4.setCellStyle(style);
		
		HSSFCell hssfCell5 = headRow.createCell(5);
		hssfCell5.setCellValue("发件人地址");
		hssfCell5.setCellStyle(style);
		
		HSSFCell hssfCell6 = headRow.createCell(6);
		hssfCell6.setCellValue("收件人姓名");
		hssfCell6.setCellStyle(style);
		
		HSSFCell hssfCell7 = headRow.createCell(7);
		hssfCell7.setCellValue("收件人电话");
		hssfCell7.setCellStyle(style);
		
		HSSFCell hssfCell8 = headRow.createCell(8);
		hssfCell8.setCellValue("收件人公司");
		hssfCell8.setCellStyle(style);
		
		HSSFCell hssfCell9 = headRow.createCell(9);
		hssfCell9.setCellValue("收件人地址");
		hssfCell9.setCellStyle(style);
		
		
		
		
		String filename = ServletActionContext.getRequest().getParameter("filename");
		HttpServletResponse response = ServletActionContext.getResponse();
		//文件下载：一个流 两个头（1、文件MIME类型  2、文件打开方式（浏览器内部打开，作为附件下载））
		String agent = ServletActionContext.getRequest().getHeader("User-Agent");
		//对文件名进行编码
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		response.setHeader("content-type", "application/vnd.ms-excel");
		response.setHeader("content-disposition", "attachment;filename="+filename);
		OutputStream stream = response.getOutputStream();
		workbook.write(stream);
		return NONE;
	}
	
	
	//定义变量接收上传文件
	private File upload;
	//接收文件MIME类型，文件名称
	private String uploadContentType;
	private String uploadFileName;
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	
	@Action("wayBillAction_importXls")
	public String importXls() throws Exception {
		List<WayBill> list=new ArrayList<>();
		//创建excel文件对象
		HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(upload));
		//获取标签页(Sheet1,Sheet2,Sheet3)
		HSSFSheet sheet = workbook.getSheetAt(0);
		//遍历标签页获取行
		for (Row row : sheet) {
			//忽略标题行
			if(row.getRowNum()==0){
				continue;
			}
			
			Integer id = null;
			if(row.getCell(0)!=null){
				row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				String id1=row.getCell(0).getStringCellValue();
				id = Integer.parseInt(id1);
			}
			
			String goodsType = null;
			if(row.getCell(1)!=null){
				row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
				goodsType = row.getCell(1).getStringCellValue();
			}
			
			String sendProNum = null;
			if(row.getCell(2)!=null){
				row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
				sendProNum = row.getCell(2).getStringCellValue();
			}
			
			String sendName = null;
			if(row.getCell(3)!=null){
				row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
				sendName = row.getCell(3).getStringCellValue();
			}
			
			String sendMobile = null;
			if(row.getCell(4)!=null){
				row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
				sendMobile = row.getCell(4).getStringCellValue();
			}
			
			String sendAddress = null;
			if(row.getCell(5)!=null){
				row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
				sendAddress = row.getCell(5).getStringCellValue();
			}
			
			String recName = null;
			if(row.getCell(6)!=null){
				row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
				recName = row.getCell(6).getStringCellValue();
			}
			
			String recMobile = null;
			if(row.getCell(7)!=null){
				row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
				recMobile = row.getCell(7).getStringCellValue();
			}
			
			String recCompany = null;
			if(row.getCell(8)!=null){
				row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
				recCompany = row.getCell(8).getStringCellValue();
			}
			
			String recAddress = null;
			if(row.getCell(9)!=null){
				row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
				recAddress = row.getCell(9).getStringCellValue();
			}
			WayBill wayBill = new WayBill(id, sendName, sendMobile, sendAddress, recName, recMobile, recCompany, recAddress, sendProNum, goodsType);
			list.add(wayBill);
		}
		wayBillService.save(list);
		//释放资源
		workbook.close();
		return NONE;
	}
	
	
	@Action("wayBillAction_pageQuery")
	public String pageQuery() throws Exception {
		Pageable pageable = new PageRequest(page-1, rows);
		Page<WayBill> page = wayBillService.pageQuery(pageable);
		this.java2Json(page, new String[]{"subareas",});
		return NONE;
	}
}
